using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The LocationTableDB class is responsible for interacting with the database to retrieve and store information
    /// about LocationTable objects.
    /// </summary>
    public class LocationTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of LocationTable from the underlying datasource.
        /// </summary>
        /// <param name="locationKey">The unique LocationKey of the LocationTable in the database.</param>
        /// <returns>An LocationTable when the LocationKey was found in the database, or null otherwise.</returns>
        public static LocationTable GetItem(int locationKey)
        {
            LocationTable myLocationTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocLocationTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@locationKey", locationKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myLocationTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myLocationTable;
            }
        }

        /// <summary>
        /// Returns a list with LocationTable objects.
        /// </summary>
        /// <returns>A generics List with the LocationTable objects.</returns>
        public static LocationTableList GetList(int SchoolKey)
        {
            LocationTableList tempList = new LocationTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocLocationTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a LocationTable in the database.
        /// </summary>
        /// <param name="myLocationTable">The LocationTable instance to save.</param>
        /// <returns>The new LocationKey if the LocationTable is new in the database or the existing LocationKey when an item was updated.</returns>
        public static int Save(LocationTable myLocationTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocLocationTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myLocationTable.LocationKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationKey", myLocationTable.LocationKey);
                    }

                    if (myLocationTable.SchoolKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", myLocationTable.SchoolKey);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.LocationCode))
                    {
                        myCommand.Parameters.AddWithValue("@locationCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationCode", myLocationTable.LocationCode);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.LocationName))
                    {
                        myCommand.Parameters.AddWithValue("@locationName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@locationName", myLocationTable.LocationName);
                    }

                    if (myLocationTable.ContactPersonKey <= 0 )
                    {
                        myCommand.Parameters.AddWithValue("@ContactPersonKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@ContactPersonKey", myLocationTable.ContactPersonKey);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.ContactNo))
                    {
                        myCommand.Parameters.AddWithValue("@ContactNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@ContactNo", myLocationTable.ContactNo);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.FaxNo))
                    {
                        myCommand.Parameters.AddWithValue("@FaxNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@FaxNo", myLocationTable.FaxNo);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.EmailAddress))
                    {
                        myCommand.Parameters.AddWithValue("@EmailAddress", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@EmailAddress", myLocationTable.EmailAddress);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.WebsiteUrl))
                    {
                        myCommand.Parameters.AddWithValue("@WebsiteUrl", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@WebsiteUrl", myLocationTable.WebsiteUrl);
                    }

                    if (string.IsNullOrEmpty(myLocationTable.Address1))
                    {
                        myCommand.Parameters.AddWithValue("@address1", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@address1", myLocationTable.Address1);
                    }
                    if (string.IsNullOrEmpty(myLocationTable.Address2))
                    {
                        myCommand.Parameters.AddWithValue("@address2", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@address2", myLocationTable.Address2);
                    }
                    if (string.IsNullOrEmpty(myLocationTable.StreetName))
                    {
                        myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@streetName", myLocationTable.StreetName);
                    }
                    if (myLocationTable.TownshipCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", myLocationTable.TownshipCode);
                    }
                    if (myLocationTable.CityCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myLocationTable.CityCode);
                    }
                    if (myLocationTable.DistrictCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myLocationTable.DistrictCode);
                    }
                    if (myLocationTable.CountryCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myLocationTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myLocationTable.PostalCode))
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", myLocationTable.PostalCode);
                    }
                    if (myLocationTable.AddressTypeCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", myLocationTable.AddressTypeCode);
                    }
                    if (string.IsNullOrEmpty(myLocationTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myLocationTable.Remark);
                    }
                    if (myLocationTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myLocationTable.Status);
                    }
                    //if (myLocationTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myLocationTable.CreatedDateTime);
                    //}
                    //if (myLocationTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myLocationTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a LocationTable from the database.
        /// </summary>
        /// <param name="locationKey">The LocationKey of the LocationTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int locationKey, out string errorMessage)
        {
            int result = 0;
            errorMessage = string.Empty;
            try
            {
                using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
                {
                    using (SqlCommand myCommand = new SqlCommand("sprocLocationTableDeleteSingleItem", myConnection))
                    {
                        myCommand.CommandType = CommandType.StoredProcedure;

                        myCommand.Parameters.AddWithValue("@locationKey", locationKey);
                        myConnection.Open();
                        result = myCommand.ExecuteNonQuery();
                        myConnection.Close();
                    }
                }
            }
            catch (Exception Ex)
            {
                errorMessage = Ex.Message;
                Ex = null;
            }

            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the LocationTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static LocationTable FillDataRecord(IDataRecord myDataRecord)
        {
            LocationTable myLocationTable = new LocationTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
            {
                myLocationTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                myLocationTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationCode")))
            {
                myLocationTable.LocationCode = myDataRecord.GetString(myDataRecord.GetOrdinal("LocationCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationName")))
            {
                myLocationTable.LocationName = myDataRecord.GetString(myDataRecord.GetOrdinal("LocationName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ContactPersonKey")))
            {
                myLocationTable.ContactPersonKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ContactPersonKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ContactNo")))
            {
                myLocationTable.ContactNo = myDataRecord.GetString(myDataRecord.GetOrdinal("ContactNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNo")))
            {
                myLocationTable.FaxNo = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
            {
                myLocationTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("WebsiteUrl")))
            {
                myLocationTable.WebsiteUrl = myDataRecord.GetString(myDataRecord.GetOrdinal("WebsiteUrl"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Address1")))
            {
                myLocationTable.Address1 = myDataRecord.GetString(myDataRecord.GetOrdinal("Address1"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Address2")))
            {
                myLocationTable.Address2 = myDataRecord.GetString(myDataRecord.GetOrdinal("Address2"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
            {
                myLocationTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
            {
                myLocationTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myLocationTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myLocationTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myLocationTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
            {
                myLocationTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
            {
                myLocationTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myLocationTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myLocationTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myLocationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myLocationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myLocationTable;
        }
    }

}
